﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Board;
using CE.User;
using TDS.Mail;

public partial class board_ask_question : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[CE.Global.General.UserId] != null)
            {
            //    liAnswer.Style.Add("display", "");
                CheckUser();
            }

            Session[GlobalSettings.CaptchaImageText] = GlobalSettings.GenerateRandomCode(4);
            hidSecurityCheck.Value = Session[GlobalSettings.CaptchaImageText].ToString();
        }
        else
        {
            lblMessage.Text = "";
            lblSubmitMessage.Text = "";
        }
    }

    private void CheckUser()
    {
        Int64 _uid = Int64.Parse(Session[CE.Global.General.UserId].ToString());
        Account _account = new Account();
        AccountDB _accountdb = new AccountDB();
        _account = _accountdb.GetAccount(_uid);

        Profile _userprofile = new Profile();
        ProfileDB _userprofiledb = new ProfileDB();
        _userprofile = _userprofiledb.GetProfile(_uid);

        ddlTitle.SelectedIndex = ddlTitle.Items.IndexOf(ddlTitle.Items.FindByText(_userprofile.Salutation));
        txtFullName.Text = _userprofile.Name;
        txtEmailAddress.Text = _account.Email;
        txtContactNumber.Text = _userprofile.ContactNumber;
    }

    protected void btnSubmitForm_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {
                if (txtSecurityCheck.Text.Trim().ToUpper() == hidSecurityCheck.Value.ToUpper())
                {
                    Board objBoard = new Board();
                    BoardDB objBoardDb = new BoardDB();

                    objBoard.Subject = txtSubject.Text;
                    objBoard.Question = txtQuestion.Text;
                    objBoard.Name = ddlTitle.SelectedItem.Text + " " + txtFullName.Text;
                    objBoard.Phone = txtContactNumber.Text;
                    objBoard.Email = txtEmailAddress.Text;
                    if (Session[CE.Global.General.UserId] != null)
                    {
                        objBoard.QuestionBy = Convert.ToInt64(Session[CE.Global.General.UserId]);
                    }
                    objBoard.SenderIP = Request.UserHostAddress;

                    if (objBoardDb.Insert(objBoard) > 0)
                    {
                        MailClient _remail = new MailClient();
                        string _toEmail = System.Configuration.ConfigurationSettings.AppSettings["TDS.BoardEmailReceiver"].ToString();
                        string _strTitle = "New question form (Full Name: " + txtFullName.Text + ") at Date/Time:" + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");

                        System.Text.StringBuilder _strBody = new System.Text.StringBuilder();
                        _strBody.Append("Dear Sir/Madam,<br/><br/>");
                        _strBody.Append("New question form " + ddlTitle.SelectedItem.Text + " " + Server.HtmlEncode(txtFullName.Text) + "<br/><br/>");
                        _strBody.Append("<table style=\"width:80%;\" cellspacing=\"5\" cellpadding=\"5\">");
                        _strBody.Append("<tr><td style=\"width:180px;\" valign=\"top\">Subject</td><td valign=\"top\">:</td><td>" + Server.HtmlEncode(txtSubject.Text) + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\" valign=\"top\">Question</td><td valign=\"top\">:</td><td>" + Server.HtmlEncode(txtQuestion.Text.Trim()) + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\">Email Address</td><td>:</td><td>" + txtEmailAddress.Text + "</td></tr>");
                        _strBody.Append("<tr><td style=\"width:180px;\">Phone Number</td><td>:</td><td>" + txtContactNumber.Text + "</td></tr>");
                        _strBody.Append("</table>");

                        if (_remail.SendNetNoreplyMailAsync(_strTitle, _strBody.ToString(), "Teledirect Contact Us", "Teledirect", _toEmail, string.Empty, string.Empty))
                        {
                            
                        }

                        Response.Redirect("list-question.aspx?say=thanks");
                    }
                    else
                    {
                        throw new Exception("");
                    }
                }
                else
                {
                    lblMessage.Text = "Wrong security check code. Please try again!";
                    lblMessage.Font.Bold = true;
                    lblMessage.ForeColor = System.Drawing.Color.Red;

                    Session[GlobalSettings.CaptchaImageText] = GlobalSettings.GenerateRandomCode(4);
                    hidSecurityCheck.Value = Session[GlobalSettings.CaptchaImageText].ToString();
                    //lblMessage.Text = "Encrypt:" + hidSecurityCheck.Value.ToString() + ", Decrypt:" + MOC.Utilities.Security.MySecurity.Decrypt(hidSecurityCheck.Value.ToString(), true);
                }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = "Error found! Please try it later again.<br/>" + Ex.Message;
                lblMessage.Font.Bold = true;
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}
